<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch">
      <el-form-item label="县区" prop="city" v-show="cityShow">
        <el-select
          v-model="queryParams.city"
          clearable
          placeholder="请选择"
          size="small"
          @change="selectCity"
        >
          <el-option
            v-for="item in this.cityData"
            :key="item.deptCode"
            :label="item.deptName"
            :value="item.deptCode">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="乡镇" prop="town" v-show="townShow">
        <el-select
          v-model="queryParams.town"
          clearable
          placeholder="请选择"
          size="small"
          @change="selectTown"
        >
          <el-option
            v-for="item in this.townData"
            :key="item.deptCode"
            :label="item.deptName"
            :value="item.deptCode">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="村" prop="village" v-show="villageShow">
        <el-select
          v-model="queryParams.village"
          clearable
          placeholder="请选择"
          size="small"
          @change="selectVillage"
        >
          <el-option
            v-for="item in this.villageData"
            :key="item.deptCode"
            :label="item.deptName"
            :value="item.deptCode">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="任务派发时间" prop="assignTime">
        <el-date-picker clearable size="small" style="width: 200px"
                        v-model="queryParams.startAssignTime"
                        type="date"
                        :picker-options="startDatePicker"
                        value-format="yyyy-MM-dd"
                        placeholder="选择指派时间起">
        </el-date-picker>
        -
        <el-date-picker clearable size="small" style="width: 200px"
                        v-model="queryParams.endAssignTime"
                        type="date"
                        :picker-options="endDatePicker"
                        value-format="yyyy-MM-dd"
                        placeholder="选择指派时间止">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="任务状态" prop="taskState">
        <el-select v-model="taskState" placeholder="请选择" clearable size="small">
          <el-option
            v-for="dict in superviseTaskState"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="cyan" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          icon="el-icon-edit"
          size="mini"
          @click="handleAdd"
          :disabled="multiple"
          v-hasPermi="['supervise:supervise:add']"
        >批量修改
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <div class="show-icon">注释：若检查时间晚于任务截止时间，则行政区划以 <span
      style="color: red">红色</span> 展示
    </div>
    <div class="show-signal"></div>
    <el-table v-loading="loading" :data="superviseList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" :selectable='checkboxList'/>
      <el-table-column width="180" label="任务编号" align="center" prop="taskNo"/>
      <el-table-column label="任务类型" align="center" prop="taskType" :formatter="taskTypeFormat"/>
      <el-table-column label="行政区划" min-width="120" align="center" prop="taskDeptCode">
        <template slot-scope="scope">
          <span
            v-if="scope.row.planCompleteTime<scope.row.checkTime||(scope.row.planCompleteTime<new Date()&&(scope.row.checkTime==''||scope.row.checkTime==null) )"
            style="color: red;">{{
              scope.row.countyName
            }}-{{ scope.row.townshipName }}-{{ scope.row.superviseDeptName }}</span>
          <span v-else>{{ scope.row.countyName }}-{{ scope.row.townshipName }}-{{ scope.row.superviseDeptName }}</span>
        </template>
      </el-table-column>
      <el-table-column label="承包方名称" align="center" prop="cbfmc"/>
          <el-table-column label="地块编码" align="center" prop="dkbm"/>
          <el-table-column label="退耕年份" align="center" prop="tyear"/>
          <el-table-column label="面积（亩）" align="center" prop="scmjm"/>
          <el-table-column label="土地性质" align="center" prop="dksx"/>
          <el-table-column label="附着物" align="center" prop="fzwlx"/>
      <el-table-column label="任务派发时间" align="center" prop="assignTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.assignTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="检查方式" align="center" prop="taskCheckType" :formatter="taskCheckTypeFormat"/>
      <el-table-column label="协调检查" align="center" prop="synergistic">
        <template slot-scope="scope">
          {{ scope.row.synergistic == 'Y' ? '是' : '否' }}
        </template>
      </el-table-column>
      <el-table-column label="任务状态" align="center" prop="taskState" :formatter="taskStateFormat"/>
      <!-- <el-table-column label="检查时间" align="center" prop="checkTime" width="180">
         <template slot-scope="scope">
           <span>{{ parseTime(scope.row.checkTime, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
         </template>
       </el-table-column>-->
      <el-table-column label="检查结果" align="center" prop="taskResultType" :formatter="taskResultTypeFormat"/>
      <el-table-column label="操作" align="center" width="100" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            v-if="scope.row.taskState != '2'"
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['supervise:supervise:edit']"
          >修改
          </el-button>
          <el-button v-if="scope.row.by2 == '0'" @click="grant(scope.row)" size="mini" type="text">授权</el-button>
          <!-- <el-button
             size="mini"
             type="text"
             icon="el-icon-delete"
             @click="handleDelete(scope.row)"
             v-hasPermi="['supervise:supervise:remove']"
           >删除
           </el-button>-->
        </template>
      </el-table-column>
    </el-table>

    <!--<el-table v-loading="loading" :data="superviseList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="编号" align="center" prop="taskNo" />
      <el-table-column label="类型" align="center" prop="taskType" />
      <el-table-column label="指派单位" align="center" prop="taskDeptCode" />
      <el-table-column label="指派时间" align="center" prop="assignTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.assignTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="截止登记日期" align="center" prop="planCompleteTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.planCompleteTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="状态" align="center" prop="taskState" />
      <el-table-column label="协同检查人" align="center" prop="coinspector" />
      <el-table-column label="检查类型" align="center" prop="taskCheckType" />
      <el-table-column label="是否协同" align="center" prop="synergistic" />
      <el-table-column label="检查结果" align="center" prop="taskResultType" />
      <el-table-column label="检查时间" align="center" prop="checkTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.checkTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="检查人" align="center" prop="examiner" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['supervise:supervise:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['supervise:supervise:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>-->

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改监督管理对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="800px" append-to-body @close="cancel">
      <el-form ref="form" :model="form" :rules="rules" label-width="130px">
        <el-form-item label="检查时间" prop="planCompleteTime">
          <el-date-picker clearable size="small" style="width: 100%"
                          v-model="form.checkTime"
                          type="date"
                          :picker-options="checkDatePicker"
                          value-format="yyyy-MM-dd"
                          placeholder="选择任检查时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="检查人" prop="examiner">
          <el-input v-model="form.examiner" placeholder="请输入检查人"/>
        </el-form-item>
        <el-form-item label="检查方式"  prop="taskCheckType">
          
          <el-select v-model="form.taskCheckType" multiple placeholder="请选择" style="width: 100%;">
            <el-option
              v-for="dict in superviseCheckType"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="dict.dictValue"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="检查结果" prop="taskResultType">
          <el-select v-model="form.taskResultType" placeholder="请选择" clearable size="small" style="width: 100%">
            <el-option
              v-for="dict in superviseCheckResult"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="dict.dictValue"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入备注"/>
        </el-form-item>
        <el-form-item label="附件" >
          <!--prop="reportfj"  -->
          <!--            class="avatar-uploader"-->
          <!--            list-type="picture-card"-->

          <el-upload
            :action="productFileAction"
            :file-list="fileList"
            :before-upload="beforeUpload"
            :headers="headers"
            :on-remove="handleRemove"
            :on-success="uploadSuccess"
                      :on-preview="handlePreview"
          :on-exceed="masterFileMax"
            :limit=limit
            multiple
            name="uploadfiles">
            <!--            accept="image/*"-->
<!--            <i class="el-icon-plus avatar-uploader-icon"></i>-->
            <el-button><i class="el-icon-plus avatar-uploader-icon"></i></el-button>
          </el-upload>
          <el-input type="textarea" v-show="1==2" v-model="form.reportfj" placeholder="请输入附加"/>
        </el-form-item>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <el-dialog :visible.sync="dialogVisible">
      <img width="100%" :src="dialogImageUrl" alt="">
    </el-dialog>

    <!--<el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
  <el-form ref="form" :model="form" :rules="rules" label-width="80px">
    <el-form-item label="任务类型 0本级 1下级" prop="taskType">
      <el-select v-model="form.taskType" placeholder="请选择任务类型 0本级 1下级">
        <el-option label="请选择字典生成" value="" />
      </el-select>
    </el-form-item>
    <el-form-item label="指派的任务单位" prop="taskDeptCode">
      <el-input v-model="form.taskDeptCode" placeholder="请输入指派的任务单位" />
    </el-form-item>
    <el-form-item label="任务指派时间" prop="assignTime">
      <el-date-picker clearable size="small" style="width: 200px"
        v-model="form.assignTime"
        type="date"
        value-format="yyyy-MM-dd"
        placeholder="选择任务指派时间">
      </el-date-picker>
    </el-form-item>
    <el-form-item label="任务截止登记日期" prop="planCompleteTime">
      <el-date-picker clearable size="small" style="width: 200px"
        v-model="form.planCompleteTime"
        type="date"
        value-format="yyyy-MM-dd"
        placeholder="选择任务截止登记日期">
      </el-date-picker>
    </el-form-item>
    <el-form-item label="任务状态 0待派发 1待执行 2已执行" prop="taskState">
      <el-input v-model="form.taskState" placeholder="请输入任务状态 0待派发 1待执行 2已执行" />
    </el-form-item>
    <el-form-item label="经度" prop="longitude">
      <el-input v-model="form.longitude" placeholder="请输入经度" />
    </el-form-item>
    <el-form-item label="协同检查人" prop="coinspector">
      <el-input v-model="form.coinspector" placeholder="请输入协同检查人" />
    </el-form-item>
    <el-form-item label="维度" prop="dimension">
      <el-input v-model="form.dimension" placeholder="请输入维度" />
    </el-form-item>
    <el-form-item label="任务检查类型 0实地 1其他" prop="taskCheckType">
      <el-select v-model="form.taskCheckType" placeholder="请选择任务检查类型 0实地 1其他">
        <el-option label="请选择字典生成" value="" />
      </el-select>
    </el-form-item>
    <el-form-item label="是否协同 Y是 N否" prop="synergistic">
      <el-input v-model="form.synergistic" placeholder="请输入是否协同 Y是 N否" />
    </el-form-item>
    <el-form-item label="检查结果 0代表合格 1代表不合格" prop="taskResultType">
      <el-select v-model="form.taskResultType" placeholder="请选择检查结果 0代表合格 1代表不合格">
        <el-option label="请选择字典生成" value="" />
      </el-select>
    </el-form-item>
    <el-form-item label="附件" prop="reportfj">
      <el-input v-model="form.reportfj" placeholder="请输入附件" />
    </el-form-item>
    <el-form-item label="检查时间" prop="checkTime">
      <el-date-picker clearable size="small" style="width: 200px"
        v-model="form.checkTime"
        type="date"
        value-format="yyyy-MM-dd"
        placeholder="选择检查时间">
      </el-date-picker>
    </el-form-item>
    <el-form-item label="备用1" prop="by1">
      <el-input v-model="form.by1" placeholder="请输入备用1" />
    </el-form-item>
    <el-form-item label="检查人" prop="examiner">
      <el-input v-model="form.examiner" placeholder="请输入检查人" />
    </el-form-item>
    <el-form-item label="备用2" prop="by2">
      <el-input v-model="form.by2" placeholder="请输入备用2" />
    </el-form-item>
    <el-form-item label="备用3" prop="by3">
      <el-input v-model="form.by3" placeholder="请输入备用3" />
    </el-form-item>
    <el-form-item label="备用4" prop="by4">
      <el-input v-model="form.by4" placeholder="请输入备用4" />
    </el-form-item>
    <el-form-item label="备用5" prop="by5">
      <el-input v-model="form.by5" placeholder="请输入备用5" />
    </el-form-item>
    <el-form-item label="删除标志" prop="delFlag">
      <el-input v-model="form.delFlag" placeholder="请输入删除标志" />
    </el-form-item>
    <el-form-item label="备注" prop="remark">
      <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" />
    </el-form-item>
  </el-form>
  <div slot="footer" class="dialog-footer">
    <el-button type="primary" @click="submitForm">确 定</el-button>
    <el-button @click="cancel">取 消</el-button>
  </div>
</el-dialog>-->
  </div>
</template>

<script>
import {delSupervise, executeSupervise, exportSupervise, getSupervise, listSupervise} from "@/api/jdgl/supervise";
import {getInfo} from "@/api/login";
import {childlistUse} from "@/api/system/dept";
import {getToken} from "@/utils/auth";

export default {
  name: "Supervise",
  data() {
    return {
      checkDatePicker: this.checkDate(),
      startDatePicker: this.beginDate(),
      endDatePicker: this.processDate(),
      dialogVisible: false,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      times: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      fileList: [],
      limit: 5,
      headers: {Authorization: 'Bearer ' + getToken()},
      productFileAction: process.env.VUE_APP_BASE_API + '/system/resource/profile/upload',
      imageTempList: [],
      imageList: "",
      productfileList: [],
      // 监督管理表格数据
      superviseList: [],
      superviseTaskType: [],
      superviseCheckResult: [],
      superviseTaskState: [],
      superviseCheckType: [],
      dialogImageUrl: '',
      deptCode: '',
      city: '',
      town: '',
      village: '',
      cityShow: '',
      townShow: '',
      villageShow: '',
      cityData: [],
      townData: [],
      villageData: [],
      synergisticData: [{name: '是', value: 'Y'}, {name: '否', value: 'N'}],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      openList: false,
      taskState: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        taskNo: null,
        taskType: null,
        taskDeptCode: null,
        assignTime: null,
        planCompleteTime: null,
        taskState: null,
        taskStates: null,
        longitude: null,
        coinspector: null,
        dimension: null,
        taskCheckType: null,
        synergistic: null,
        taskResultType: null,
        reportfj: null,
        checkTime: null,
        startAssignTime: null,
        endAssignTime: null,
        by1: null,
        examiner: null,
        by2: null,
        by3: null,
        by4: null,
        by5: null,
      },
      // 表单参数
      form: {
      },
      // 表单校验
      rules: {
        checkTime: [
          {required: true, message: "检查时间不能为空", trigger: "change"}
        ],
        // reportfj: [
        //   {required: true, message: "附件不能为空", trigger: "change"}
        // ],
        examiner: [
          {required: true, message: "检查人不能为空", trigger: "blur"}
        ],
        taskResultType: [
          {required: true, message: "检查结果不能为空", trigger: "blur"}
        ],
        taskCheckType: [
          {required: true, message: "检查方式不能为空", trigger: "change"}
        ],
      }
    };
  },
  created() {
    this.getDicts('supervise_task_type').then(response => {
      this.superviseTaskType = response.data
    })
    this.getDicts('supervise_check_type').then(response => {
      this.superviseCheckType = response.data
    })
    this.getDicts('supervise_check_result').then(response => {
      this.superviseCheckResult = response.data
    })
    this.getDicts('supervise_task_state').then(response => {
      this.superviseTaskState = response.data
    })

    getInfo().then(response => {
      let deptCode = response.user.dept.deptCode;
      this.deptCode = deptCode;
      this.getList();
      if (deptCode.length == 4) {//新区
        this.cityShow = true;
        this.townShow = true;
        this.villageShow = true;
        childlistUse(deptCode).then(response => {
          this.cityData = response.data;
        });
      } else if (deptCode.length == 6) {//县
        this.cityShow = false;
        this.townShow = true;
        this.villageShow = true;
        childlistUse(deptCode).then(response => {
          this.townData = response.data;
        });
      } else if (deptCode.length == 9) {//镇
        this.cityShow = false;
        this.townShow = false;
        this.villageShow = true;
        childlistUse(deptCode).then(response => {
          this.villageData = response.data;
        });
      } else if (deptCode.length == 12) {//村
        this.cityShow = false;
        this.townShow = false;
        this.villageShow = false;
      } else {
        this.childlist(1306);
        this.cityShow = true;
        this.townShow = true;
        this.villageShow = true;
      }
    });
  },
  methods: {
    checkDate() {
      let _this = this;
      return {
        disabledDate(time) {
          let assignTime = _this.times;
          if (assignTime != '' && assignTime != null) {  //如果结束时间不为空，则小于结束时间
            return new Date(assignTime).getTime() > time.getTime();
          } else {
            return false;
          }
        }
      }
    },
    beginDate() {
      const self = this
      return {
        disabledDate(time) {
          if (self.queryParams.endAssignTime != '' && self.queryParams.endAssignTime != null) {  //如果结束时间不为空，则小于结束时间
            return new Date(self.queryParams.endAssignTime).getTime() < time.getTime();
          } else {
            return false;
          }
        }
      }
    },
    processDate() {
      const self = this
      return {
        disabledDate(time) {
          if (self.queryParams.startAssignTime != '' && self.queryParams.startAssignTime != null) {  //如果开始时间不为空，则结束时间大于开始时间
            return new Date(self.queryParams.startAssignTime).getTime() > time.getTime();
          } else {
            return false;
          }
        }
      }
    },
    checkboxList(row, rowIndex) {
      if (row.taskState != '2') {
        return true;//禁用
      } else {
        return false;//不禁用
      }
    },
    taskCheckTypeFormat(row, column) {
      let arr =  row.taskCheckType.split(',')
      let arrxin = [];
      for(let i = 0 ; i < arr.length; i++){
        for(let o = 0; o < this.superviseCheckType.length ; o++){
          if(arr[i] == this.superviseCheckType[o].dictValue){
            arrxin.push(this.superviseCheckType[o].dictLabel)
          }
        }
      }

      return arrxin.toString();
      // return this.selectDictLabel(this.superviseCheckType, row.taskCheckType);
    },
    synergisticFormat(row, column) {
      return this.selectDictLabel(this.superviseCheckType, row.synergistic);
    },
    handlePreview(file) {
      if (window.navigator.msSaveOrOpenBlob) {
        navigator.msSaveBlob(file.raw, file.name)
      } else {
        let link = document.createElement('a')
        link.download = file.name
        link.href = window.URL.createObjectURL(file.raw)
        link.click()
        window.URL.revokeObjectURL(link.href)
      }
      /*this.dialogImageUrl = file.url;
      this.dialogVisible = true;*/
      // window.open(file.response.data[0]);
    },
    handleExceed(files, fileList) {
      this.$message.warning(`当前限制选择 3 个文件，本次选择了 ${files.length} 个文件，共选择了 ${files.length + fileList.length} 个文件`);
    },
    beforeRemove(file, fileList) {
      return this.$confirm(`确定移除 ${file.name}？`);
    },
    handleRemove(file, fileList) {
      this.$confirm('是否确认删除附件吗?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(function () {
        this.imageTempList = [];
        this.imageList = '';
        if (fileList.length < 1) {
          this.form.reportfj = "";
        }
        fileList.forEach(item => {
          if (item.url.indexOf("blob:") > -1) {
            let url = process.env.VUE_APP_BASE_API + item.response.data[0];
            let imageInfo = {name: file.name, url: url};
            this.imageTempList.push(imageInfo);
            if (this.imageList == "") {
              this.imageList = this.imageList + url;
              this.form.reportfj = this.imageList;
            } else {
              this.imageList = this.imageList + "," + url;
              this.form.reportfj = this.imageList;
            }
          } else {
            let url = item.url;
            let imageInfo = {name: item.name, url: url};
            this.imageTempList.push(imageInfo);
            if (this.imageList == "") {
              this.imageList = this.imageList + url;
              this.form.reportfj = this.imageList;
            } else {
              this.imageList = this.imageList + "," + url;
              this.form.reportfj = this.imageList;
            }
          }
        });
        // return delSupervise(superviseIds);
      }).then(() => {
        this.getList();
        this.msgSuccess("删除成功");
      })
    },
    beforeUpload(file) {
      // 图片显示前做一下判断
      // let isJPG = /^image\/(jpeg|png|jpg)$/.test(file.type);
      let name = file.name.split(".");
      let fileType = name[name.length - 1];
      // 图片显示前做一下判断
      let isJPG = "jpeg|png|jpg".indexOf(fileType) > -1;
      const isLt5M = file.size / 1024 / 1024 < 5;
      // if (!isJPG) {
      //   this.$message.error('上传图片格式不对,只允许上传jpeg、png、jpg格式!');
      // }
      if (!isLt5M) {
        this.$message.error('上传头像图片大小不能超过 5MB!');
      }
      // return isJPG && isLt5M;
      return isLt5M;
    },
    uploadSuccess(response, file, fileList) {
      this.imageTempList = [];
      this.imageList = '';
      if (response.code == 200) {
        fileList.forEach(item => {
          if (item.status == "success") {
            let url = process.env.VUE_APP_BASE_API + item.response.data[0];
            let imageInfo = {name: file.name, url: url};
            this.imageTempList.push(imageInfo);
            if (this.imageList == "") {
              this.imageList = this.imageList + url;
              this.form.reportfj = this.imageList;
            } else {
              this.imageList = this.imageList + "," + url;
              this.form.reportfj = this.imageList;
            }
          }
          /*if (item.url.indexOf("blob:") > -1) {
            if (item.status == "success") {
              let url = process.env.VUE_APP_BASE_API + item.response.data[0];
              let imageInfo = {name: file.name, url: url};
              this.imageTempList.push(imageInfo);
              if (this.imageList == "") {
                this.imageList = this.imageList + url;
                this.form.reportfj = this.imageList;
              } else {
                this.imageList = this.imageList + "," + url;
                this.form.reportfj = this.imageList;
              }
            }
          } else {
            let url = item.url;
            let imageInfo = {name: item.name, url: url};
            this.imageTempList.push(imageInfo);
            if (this.imageList == "") {
              this.imageList = this.imageList + url;
              this.form.reportfj = this.imageList;
            } else {
              this.imageList = this.imageList + "," + url;
              this.form.reportfj = this.imageList;
            }
          }*/
        });
        // this.timer = setInterval(this.getImageList(fileList), 1000);
      } else {
        this.$message.error(response.msg)
      }

      /*if (response.code == 200) {
        fileList.forEach(item => {
          if (item.url.indexOf("blob:") > -1) {
            if (item.status == "success") {
              let url = process.env.VUE_APP_BASE_API + item.response.data[0];
              let imageInfo = {name: file.name, url: url};
              this.imageTempList.push(imageInfo);
              if (this.imageList == "") {
                this.imageList = this.imageList + url;
                this.form.reportfj = this.imageList;
              } else {
                this.imageList = this.imageList + "," + url;
                this.form.reportfj = this.imageList;
              }
            }
          } else {
            let url = item.url;
            let imageInfo = {name: item.name, url: url};
            this.imageTempList.push(imageInfo);
            if (this.imageList == "") {
              this.imageList = this.imageList + url;
              this.form.reportfj = this.imageList;
            } else {
              this.imageList = this.imageList + "," + url;
              this.form.reportfj = this.imageList;
            }
          }
        });
        // this.timer = setInterval(this.getImageList(fileList), 1000);
      } else {
        this.$message.error(response.msg)
      }*/
    },
    masterFileMax(files, fileList) {
      this.$message.warning(`最多只允许上传 ${this.limit} 个附件。`);
    },
    getImageList(fileList) {
      if (this.imageTempList.length == fileList.length) {
        this.fileList = this.imageTempList;
        clearInterval(this.timer);
      }
    },
    // 选择县
    selectCity(val) {
      this.city = val;
      this.queryParams.cbfbm = val;
      this.queryParams.superviseDeptCode = val;

      if (val != "" && val != null) {
        childlistUse(val).then(response => {
          this.townData = response.data;
        });
      }
    },
    // 选择镇
    selectTown(val) {
      this.town = val;
      this.queryParams.cbfbm = val;
      this.queryParams.superviseDeptCode = val;
      if (val != "" && val != null) {
        childlistUse(val).then(response => {
          this.villageData = response.data;
        });
      }
    },
    // 选择村
    selectVillage(val) {
      this.queryParams.cbfbm = val;
      this.queryParams.superviseDeptCode = val;
      this.village = val;
    },
    // 字典状态字典翻译
    taskStateFormat(row, column) {
      return this.selectDictLabel(this.superviseTaskState, row.taskState);
    },
    taskTypeFormat(row, column) {
      return this.selectDictLabel(this.superviseTaskType, row.taskType);
    },
    taskResultTypeFormat(row, column) {
      return this.selectDictLabel(this.superviseCheckResult, row.taskResultType);
    },
    /** 查询监督管理列表 */
    getList() {
      this.loading = true;
      let _this = this;
      this.queryParams.taskStates = '0,1';
      this.queryParams.taskDeptCode = this.deptCode;
      if (this.taskState == '' || this.taskState == null) {
        this.queryParams.taskStates = '0,1';
      }
      let taskStates = this.taskState;
      this.queryParams.taskStates = taskStates.toString();
      listSupervise(this.queryParams).then(response => {
        this.superviseList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.fileList = [];
      this.imageList = '';
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        superviseId: null,
        taskNo: null,
        taskType: null,
        taskDeptCode: null,
        assignTime: null,
        planCompleteTime: null,
        taskState: null,
        longitude: null,
        coinspector: null,
        dimension: null,
        taskCheckType: null,
        synergistic: null,
        taskResultType: null,
        reportfj: null,
        checkTime: null,
        by1: null,
        examiner: null,
        by2: null,
        by3: null,
        by4: null,
        by5: null,
        delFlag: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      let startAssignTime = this.queryParams.startAssignTime;
      let endAssignTime = this.queryParams.endAssignTime;
      startAssignTime = startAssignTime == '' || startAssignTime == null ? "" : startAssignTime;
      endAssignTime = endAssignTime == '' || endAssignTime == null ? "" : endAssignTime;
      if ((startAssignTime == '' && endAssignTime != '') || (startAssignTime != '' && endAssignTime == '')) {
        this.$message.warning("请补全任务派发起止时间");
      } else {
        this.getList();
      }
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.taskState = [];
      this.queryParams.city = '';
      this.queryParams.town = '';
      this.queryParams.village = '';
      this.queryParams.superviseDeptCode = '';
      this.queryParams.startAssignTime = '';
      this.queryParams.endAssignTime = '';
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.superviseId);
      let times = selection.map(item => item.assignTime);
      let assignTime = '';
      let len = times.length;
      let i = 0;
      times.forEach(item => {
        i++;
        if (assignTime == '') {
          assignTime = item;
          if (len == i) {
            this.times = assignTime;
          }
        } else if (new Date(assignTime) > new Date(item)) {
          assignTime = item;
          if (len == i) {
            this.times = assignTime;
          }
        }
      });
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "修改监督管理";
    },
    handleSendTask() {
      this.reset();
      this.open = true;
      // this.open = true;
      this.title = "任务派发";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      this.ids = [];
      this.times = [];
      const superviseId = row.superviseId || this.ids;
      const assignTime = row.assignTime;
      this.ids.push(superviseId);
      this.times = assignTime;
      getSupervise(superviseId).then(response => {

        response.data = response.data.taskCheckType.split(',')
        this.form = response.data;
        let htfj = response.data.reportfj;
        if (htfj != "" && htfj != null && htfj != undefined) {
          this.imageList = htfj;
          htfj = response.data.reportfj.split(',');
          htfj.forEach((item, i) => {
            let name = item.split('/')[item.split('/').length - 1];
            let imageInfo = {name: name, url: item};
            this.fileList.push(imageInfo);
          });
        }

        this.open = true;
        this.title = "修改监督管理";
      });
    },
    /** 提交按钮 */
    submitForm() {
      let _this = this;
      var a = _this.form.taskCheckType;
      var b = a.toString();
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (_this.form.taskResultType == '1') {
            if (_this.form.remark == '' || _this.form.remark == null) {
              _this.$message.error("备注不可为空");
            } else {
              executeSupervise({
                keys: this.ids,
                body: {
                  checkTime: this.form.checkTime,
                  examiner: this.form.examiner,
                  taskCheckType: b,
                  taskResultType: this.form.taskResultType,
                  remark: this.form.remark,
                  reportfj: this.form.reportfj
                }
              }).then(response => {
                _this.msgSuccess("操作成功");
                _this.open = false;
                _this.getList();
                _this.fileList = [];
                _this.imageList = '';
                this.reset();
              });
            }
          } else {
            executeSupervise({
              keys: this.ids,
              body: {
                checkTime: this.form.checkTime,
                examiner: this.form.examiner,
                taskCheckType: b,
                taskResultType: this.form.taskResultType,
                remark: this.form.remark,
                reportfj: this.form.reportfj
              }
            }).then(response => {
              _this.msgSuccess("操作成功");
              _this.open = false;
              _this.getList();
              _this.fileList = [];
              _this.imageList = '';
              this.reset();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const superviseIds = row.superviseId || this.ids;
      this.$confirm('是否确认删除监督管理编号为"' + superviseIds + '"的数据项?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(function () {
        return delSupervise(superviseIds);
      }).then(() => {
        this.getList();
        this.msgSuccess("删除成功");
      })
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有监督管理数据项?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(function () {
        return exportSupervise(queryParams);
      }).then(response => {
        this.download(response.msg);
      })
    },
    /**
     * 授权
     */
    grant(row){
      this.$axios({
         url: '/supervise/supervise/updateSupervise',
         method: 'post',
         data: {superviseId: row.superviseId, by2: '1'}
       }).then((res)=>{
         this.handleQuery();
         this.$message.success('授权成功')
       })
    },
    q(e){
      console.log(e)
    }
  }
};
</script>
<style>
.myTitle {
  margin: 10px 0;
  color: #1890FF;
  font-size: 16px;
}

.show-icon {
  text-align: center;
  background-color: #5aaafa;
  color: #fff;
  float: right;
  font-size: 12px;
  margin-right: -8px;
  padding: 8px 5px 8px 5px;
  width: 400px;
}

.show-signal {
  border: 4px solid #939393;
  border-right: 4px solid transparent;
  border-bottom: 4px solid transparent;
  float: right;
  margin-right: -400px;
  margin-top: 29px;
  width: 0;
  height: 0;
}
</style>
